using System.Data.Common;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace FwProject.EntityFrameworkCore
{
    public static class FwProjectDbContextConfigurer
    {
        private static ILoggerFactory Mlogger => new Microsoft.Extensions.Logging.LoggerFactory()
                .AddDebug((categoryName, logLevel) => (logLevel == LogLevel.Information) && (categoryName == DbLoggerCategory.Database.Command.Name))
               .AddConsole((categoryName, logLevel) => (logLevel == LogLevel.Information) && (categoryName == DbLoggerCategory.Database.Command.Name));
              //.AddTraceSource(new SourceSwitch(nameof(FwProjectDbContextConfigurer), "Warning"), new TraceSourceListener());

        public static void Configure(DbContextOptionsBuilder<FwProjectDbContext> builder, string connectionString)
        {
           #if DEBUG
             builder.UseLoggerFactory(Mlogger).UseSqlServer(connectionString, b => b.UseRowNumberForPaging());
            
        #else
             builder.UseSqlServer(connectionString, b => b.UseRowNumberForPaging());
       #endif
        }

        public static void Configure(DbContextOptionsBuilder<FwProjectDbContext> builder, DbConnection connection)
        {
             #if DEBUG
            builder.UseLoggerFactory(Mlogger).UseSqlServer(connection,b=>b.UseRowNumberForPaging());
            #else
              builder.UseSqlServer(connection,b=>b.UseRowNumberForPaging());
            #endif
        }
    }
    public class TraceSourceListener : TraceListener
    {
        public override void Write(string message) { var m = message; }
        public override void WriteLine(string message) { var m = message; }
    }
      
}
